#!/bin/bash

NACOS="http://localhost:8848"

echo "============================================================"
echo "🧪 Nacos 功能测试"
echo "============================================================"
echo ""

echo "1️⃣  发布配置..."
curl -s -X POST "${NACOS}/nacos/v1/cs/configs" \
  -d "dataId=test.properties" \
  -d "group=DEFAULT_GROUP" \
  -d "content=app.name=测试应用
app.version=1.0.0
server.port=8080"
echo ""
echo ""

echo "2️⃣  获取配置..."
echo "   配置内容:"
curl -s -X GET "${NACOS}/nacos/v1/cs/configs?dataId=test.properties&group=DEFAULT_GROUP"
echo ""
echo ""

echo "3️⃣  注册服务..."
curl -s -X POST "${NACOS}/nacos/v1/ns/instance" \
  -d "serviceName=test-service" \
  -d "ip=127.0.0.1" \
  -d "port=8080"
echo ""
echo ""

echo "4️⃣  查询服务实例..."
sleep 2
curl -s -X GET "${NACOS}/nacos/v1/ns/instance/list?serviceName=test-service" | python3 -m json.tool 2>/dev/null || curl -s -X GET "${NACOS}/nacos/v1/ns/instance/list?serviceName=test-service"
echo ""
echo ""

echo "5️⃣  删除服务..."
curl -s -X DELETE "${NACOS}/nacos/v1/ns/instance" \
  -d "serviceName=test-service" \
  -d "ip=127.0.0.1" \
  -d "port=8080"
echo ""
echo ""

echo "6️⃣  删除配置..."
curl -s -X DELETE "${NACOS}/nacos/v1/cs/configs?dataId=test.properties&group=DEFAULT_GROUP"
echo ""
echo ""

echo "============================================================"
echo "✅ Nacos 功能测试完成！"
echo "============================================================"
echo ""
echo "📊 测试总结:"
echo "  ✅ 配置管理功能正常"
echo "  ✅ 服务发现功能正常"
echo "  ✅ Nacos 服务运行正常"
echo ""
echo "�� 访问控制台: http://localhost:8848/nacos"
echo "   用户名: nacos"
echo "   密码: nacos"
echo ""
